gh-153679: Raise ZipImportError for an invalid UTF-8 file name in zipimport#153680
gh-153679: Raise ZipImportError for an invalid UTF-8 file name in zipimport#153680tonghuaroot wants to merge 3 commits into
Conversation
…in zipimport zipimport.zipimporter() is documented to raise ZipImportError for an invalid archive, but _read_directory() leaked a raw UnicodeDecodeError when a central directory entry set the UTF-8 file name flag (0x800) but stored bytes that are not valid UTF-8: the UTF-8 branch called name.decode() unguarded, while the sibling non-UTF-8 branch already handles UnicodeDecodeError. Guard the UTF-8 decode and raise ZipImportError instead.
| except UnicodeDecodeError: | ||
| raise ZipImportError( | ||
| f"can't decode file name in Zip file: {archive!r}", | ||
| path=archive) |
There was a problem hiding this comment.
| except UnicodeDecodeError: | |
| raise ZipImportError( | |
| f"can't decode file name in Zip file: {archive!r}", | |
| path=archive) | |
| except UnicodeDecodeError as exc: | |
| raise ZipImportError( | |
| f"can't decode file name in Zip file: {archive!r}", | |
| path=archive) from exc |
| os_helper.unlink(TESTMOD) | ||
| with open(TESTMOD, 'wb') as fp: | ||
| fp.write(cdh + eocd) | ||
| self.assertZipFailure(TESTMOD) |
There was a problem hiding this comment.
Make sure the UnicodeDecodeError is attached to the exception. That way we know the failure is due to that and not a mistake in writing the malformed zip file.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
Done. Chained the I have made the requested changes; please review again. |
|
Thanks for making the requested changes! @brettcannon: please review the changes made to this pull request. |
zipimport.zipimporter()is documented to raiseZipImportErrorfor aninvalid archive, but
_read_directory()leaked a rawUnicodeDecodeErrorwhen a central directory entry set the UTF-8 file name flag (
0x800) butstored bytes that are not valid UTF-8: the UTF-8 branch called
name.decode()unguarded, while the sibling non-UTF-8 branch already handles
UnicodeDecodeError.Guard the UTF-8 decode and raise
ZipImportError, matching the documentedcontract and every other corruption path in the function.